package net.lab1024.sa.admin.module.business.tests.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.tests.dao.TestsDao;
import net.lab1024.sa.admin.module.business.tests.domain.entity.TestsEntity;
import net.lab1024.sa.admin.module.business.tests.domain.form.TestsAddForm;
import net.lab1024.sa.admin.module.business.tests.domain.form.TestsQueryForm;
import net.lab1024.sa.admin.module.business.tests.domain.form.TestsUpdateForm;
import net.lab1024.sa.admin.module.business.tests.domain.vo.TestsVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * cc Service
 *
 * @Author zh
 * @Date 2024-08-20 16:01:05
 * @Copyright zz
 */

@Service
public class TestsService {

    @Resource
    private TestsDao testsDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<TestsVO> queryPage(TestsQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<TestsVO> list = testsDao.queryPage(page, queryForm);
        PageResult<TestsVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(TestsAddForm addForm) {
        TestsEntity testsEntity = SmartBeanUtil.copy(addForm, TestsEntity.class);
        testsDao.insert(testsEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(TestsUpdateForm updateForm) {
        TestsEntity testsEntity = SmartBeanUtil.copy(updateForm, TestsEntity.class);
        testsDao.updateById(testsEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        testsDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        testsDao.deleteById(id);
        return ResponseDTO.ok();
    }
}
